package com.jf.demo.composeapp

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedButton
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.jf.demo.composeapp.ui.view.MyContent
import com.jf.demo.composeapp.ui.view.MyTopBarCenter

class ListActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ListActivityContent {
                finish()
            }
        }
    }
}

data class Message(val title: String, val msg: String)

@Composable
fun ListActivityContent(backClick: (() -> Unit)? = null) {
    MyContent(
        topBar = { MyTopBarCenter("ListAct", Icons.Default.ArrowBack, onMenuClicked = backClick) }
    ) {
        ShowList()
    }
}

@Composable
fun ShowList() {
    val listData = List(30) { "$it" }
    LazyColumn(modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp)) {
        items(items = listData) { item ->
            ShowListItem(item = item)
        }
    }
}

@Composable
fun ShowListItem(item: String) {
    var expanding by remember { mutableStateOf(false) }
    val height = if (expanding) 40.dp else 80.dp
    Row(
        modifier = Modifier
            .padding(vertical = 2.dp)
            .background(MaterialTheme.colors.primary)
            .fillMaxWidth()
            .height(height),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(text = "item-$item", modifier = Modifier.weight(1f), color = Color.White)
        OutlinedButton(onClick = { expanding = !expanding }) {
            Text(
                text = if (expanding) "Show More" else "Show Less",
            )
        }
    }
}

@Preview
@Composable
fun PreviewListActivityContent() {
    ListActivityContent()
}